#coding=utf-8
import time
import logging
_logger = logging.getLogger(__name__)

def slog(s, ss):
    _logger.info("\n\n========\n%s: %s\n==============\n\n"%(s, ss))


logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S',
                    filename='myapp.log',
                    filemode='w')

def hello():
    print '==============hello======welcome to our world===='

def haha():
    print '==============hello======we wait you  come to our world again===='

def time_spend3(firstfunc,secfunc):
    def good_thing_outer(func):
        def time_spend_inner(*args, **kwargs):
            firstfunc
            timestart = time.time()
            func_return = func(*args, **kwargs)
            timeend = time.time()
            time_spend_time = timeend - timestart
            logging.info('this %s spend time %d' % (func.__name__, time_spend_time))
            slog('this function spend time', time_spend_time)
            print '=========外层=======%f'%(time_spend_time)
            secfunc
            return func_return

        return time_spend_inner
    return good_thing_outer

def time_spend2(func):
    def time_spend_inner(*args, **kwargs):
        timestart = time.time()
        func_return = func(*args, **kwargs)
        timeend = time.time()
        time_spend_time = timeend - timestart
        logging.info('this %s spend time %d' % (func.__name__, time_spend_time))
        slog('this function spend time', time_spend_time)
        print '=========内层=======%f'%(time_spend_time)
        return func_return

    return time_spend_inner


def time_spend(func):
    def time_spend_inner(*args,**kwargs):
        timestart = time.time()
        func_return = func(*args,**kwargs)
        timeend = time.time()
        time_spend_time = timeend - timestart
        logging.info('this %s spend time %d'%(func.__name__, time_spend_time))
        slog('this function spend time',time_spend_time)
        print time_spend_time
        return func_return
    return time_spend_inner

@time_spend3('hello','haha')
@time_spend2
@time_spend
def sum(n):
    a=0
    while n>0:
        n-=1
    print n
    return n

# sum(100)

@time_spend3(hello(),haha())
@time_spend2
@time_spend
def sum(n):
    a=0
    while n>0:
        n-=1
    return n

sum(100)
